#ifndef BLUETOOTHMANAGER_H
#define BLUETOOTHMANAGER_H

#include <QObject>
#include <QDebug>
#include <QTimer>
#include <QTime>

#include "bluetoothserver.h"
#include "bluetoothclient.h"

#include <qbluetoothsocket.h>

class BluetoothManager : public QObject
{
Q_OBJECT
public:
    BluetoothManager();
    ~BluetoothManager();

    Q_INVOKABLE void createServer();
    Q_INVOKABLE void stopServer();

    Q_INVOKABLE void createClient();
    Q_INVOKABLE void stopClient();

    Q_INVOKABLE void updatePosition(int x, int y);
    Q_INVOKABLE void throwBall(int x, int y, int speedX, int speedY);

    Q_INVOKABLE void lostLife();
    Q_INVOKABLE void enemyLostLife();

    Q_INVOKABLE bool isServer();
    Q_INVOKABLE void updateScores(int my, int enemy);

    void sendMessage(const QString &message);

private:
    BluetoothServer bluetoothServer;
    BluetoothClient bluetoothClient;
    QBluetoothSocket* socket;

    bool connected;
    bool server;

    bool lostLifeActivated;
    bool enemyLostLifeActivated;

    QTimer lifeTimer;

signals:
    void messageReceived(QString message);

    void noServiceFound();
    void socketConnected(QBluetoothSocket*);
    void socketDisconnected();

    void serverModeSet();

    void enemyLostLifeEvent();
    void lostLifeEvent();
    void updateScoreEvent(int myScore, int enemyScore);

    void clientConnected(); // for QML
    void clientDisonnected(); // for QML

    void updatePositionEvent(int positionX, int positionY);
    void throwBallEvent(int positionX, int positionY, int speedX, int speedY);


private slots:
    void handleConnected(QBluetoothSocket*);
    void handleDisconnected();
    void handleMessage(QString message);
    void handleLifeTimer();

};

#endif // BLUETOOTHMANAGER_H
